package com.mcarthur.countdown.client.drawable;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

import com.googlecode.gwtgl.binding.WebGLRenderingContext;
import com.mcarthur.countdown.client.renderer.CountdownRenderer;

public class DefaultAnimator extends AbstractBallAnimator {
    private static final float GRAVITY = 0.001f;  //.8f
    
    public DefaultAnimator() {
        this(null,null);
    }
    
    public DefaultAnimator(Queue<BouncyBall> activeBalls, Stack<BouncyBall> inactiveBalls) {
        super(activeBalls, inactiveBalls);
    }

    @Override
    public void draw(WebGLRenderingContext gl, long dt) {
        for (Iterator<BouncyBall> it = activeBalls.iterator(); it.hasNext();) {
            BouncyBall ball = it.next();

            if (ball.getPosX() <= -Ball.WIDTH || ball.getPosX() >= CountdownRenderer.viewWidth) {
                it.remove();
                inactiveBalls.push(ball);
                continue;
            }

            ball.setVelY(ball.getVelY() - (GRAVITY * dt));

            ball.setPosX(ball.getPosX() + (ball.getVelX() * dt));
            ball.setPosY(ball.getPosY() + (ball.getVelY() * dt));

            checkBounds(ball);
            checkCollision(ball);

            ball.draw(gl);
        }
        collidedBalls.clear();
    }
    
    @Override
    public void stop() {
        // TODO Auto-generated method stub
        
    }
}
